#!/bin/bash
username=""
title=""
fname=""
editor=""

#Prompt user for basic information
if [ ! -d "public_html" ]; then
	echo "Welcome to nci webscript! Let's start!"
else
	yn=""
	while true; do
		echo "nci is already initialized, so if you continue, you'd be only updating your configuration files. Do you want to change nci configuration? (y/n):"
		read -r yn
		case $yn in
		[Yy]* ) echo "Okay, let's continue then..."
			break
			;;
		[Nn]* ) echo "Operation cancelled."
			exit 1
			;;
		* ) echo "Error: Please answer just y/n.";;
		esac
	done
fi
echo "Please tell me, what's your neocities.org username? (just your username, I don't need the whole URL):"
read -r username
while [ -z "$username" ]; do
	echo "Dude, you just typed nothing. Please try again:"
	read -r username
done
if [ ! -d "public_html" ]; then
	echo "Okay, now please give your blog a name (i.e.: \"jane doe's super fuckin awesome blog\"):"
	read -r title
	while [ -z "$title" ]; do
		echo "No ideas? Don't worry, just type any crap for now (you'll be able to change your blog title whenever you want by editing the HTML files):"
		read -r title
	done
fi
echo "Now, I'd like to know: which text editor do you prefer? Type its name here! (lowercase, i.e.: vim, gedit, ...). Leave the line empty to use default (nano):"
read -r editor
if [ -z "$editor" ]; then
	editor="nano"
fi
echo "$editor selected as preferred editor. You can change this preference by either editing the \"user.conf\" file or running \"./init\" again."
echo "It's almost done! Please type a filename for your blog html file (lowercase, without \".html\" extension at filename end. If your blog file is your main/index page, just type \"index\"):"
read -r fname
while [ -z "$fname" ]; do
	echo "Just type \"index\" if your blog is your main page (If not, try naming it... uhm... \"blog\"?):"
	read -r fname
done

echo "Initializing nci webscript for Neocities..."
#Create the entries directory for raw entry data
if [ ! -d "entries" ]; then
	mkdir entries
	echo "\"entries\" directory created."
else
	echo "\"entries\" directory already exists."
fi

#Create the public_html directory
if [ ! -d "public_html" ]; then
	mkdir public_html
	echo "\"public_html\" directory created."
else
	echo "\"public_html\" directory already exists."
fi

#Create the blog html file
if [ ! -f "public_html/$fname.html" ]; then
	echo "<!doctype html>
<html>
<head>
<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">
<meta name=\"author\" content=\"$username\">
<link rel=\"shortcut icon\" href=\"favicon.ico\">
<link rel=\"stylesheet\" href=\"style.css\">
<title>$title</title>
</head>
<body>

<header>
<center><h1>$title</h1></center>
</header>
<section id=\"about\" align=\"justify\">
<center>This is the $username's neocities blog!</center>
</section>

<hr>
<main>
<section id=\"entries\">
<!-- !!BLOG ENTRIES -->
</section>
</main>
<hr>

<footer>
<center>- <a href=\"rss.xml\">RSS</a> -</center>
</footer>

</body>
</html>
	" >> "public_html/$fname.html"
	echo "\"public_html/$fname.html\" file created."
else
	echo "\"public_html/$fname.html\" file already exists."
fi

#Create the blog RSS feed
if [ ! -f "public_html/rss.xml" ]; then
	echo "<?xml version=\"1.0\" encoding=\"utf-8\" ?>
<rss version=\"2.0\" xmlns:atom=\"https://www.w3.org/2005/Atom/\">
<channel>
<title>$title - RSS feed</title>
<link>https://$username.neocities.org/</link>
<atom:link href=\"https://$username.neocities.org/rss.xml\" rel=\"self\" type=\"application/rss+xml\"/>
<description>This is the $username's blog RSS feed.</description>
<language>en-us</language>
<ttl>1000</ttl>
<!-- !!BLOG ENTRIES -->
</channel>
</rss>
	" >> public_html/rss.xml
	echo "\"public_html/rss.xml\" file created."
else
	echo "\"public_html/rss.xml\" file already exists."
fi

#Create style file for the blog page
if [ ! -f "public_html/style.css" ]; then
	echo "/* Write some nice CSS rules for your blog here... */" >> public_html/style.css
else
	echo "\"public_html/style.css\" file already exists."
fi

#Create entries warning file
if [ ! -f "entries/README" ]; then
	echo "DO NOT DELETE ANY FILE FROM THIS DIRECTORY (EVEN FILES THAT BELONG TO DELETED ENTRIES). DO NOT DELETE THIS FILE EITHER." >> entries/README
fi

#Create articles directory for entries html files
if [ ! -d "public_html/articles" ]; then
	mkdir "public_html/articles"
	echo "\"public_html/articles\" directory created."
else
	echo "\"public_html/articles\" directory already exists."
fi

#Create new HTML template file for articles (DO NOT DELETE IT)
if [ ! -f "public_html/articles/article_template.html" ]; then
	touch "public_html/articles/article_template.html"
	echo "<!doctype html>
<html>
<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">
<meta name=\"author\" content=\"$username\">
<link rel=\"shortcut icon\" href=\"favicon.ico\">
<link rel=\"stylesheet\" href=\"style.css\">
<!-- article title is inserted next to the title tag, you can delete this line if you want -->
<title> - $title</title>
</head>
<body>

<hr>
<main>
<!-- !!ARTICLE -->
</main>
<hr>

<footer>
<center>
<a href=\"../$fname.html\">go back</a>
</center>
</footer>

</body>
</html>
" >> public_html/articles/article_template.html
	echo "\"public_html/articles/article_template.html\" file created (DO NOT DELETE IT!)"
else
	echo "\"public_html/articles/article_template.html\" file already exists."
fi

#Create css style for articles
if [ ! -f "public_html/articles/style.css" ]; then
	touch "public_html/articles/style.css"
	echo "/* WRITE SOME CSS RULES FOR YOUR ARTICLES HERE */" >> public_html/articles/style.css
	echo "\"public_html/articles/style.css\" file created."
else
	echo "\"public_html/articles/style.css\" file already exists."
fi


#Create configuration file (DO NOT DELETE IT)
if [ ! -f "nci.conf" ]; then
	echo "$username
$fname
" >> nci.conf
	echo "Configuration file (\"nci.conf\") created! (DO NOT DELETE IT!)"
else
	echo "Configuration file already exists. Updating it..."
	rm -f "nci.conf"
	touch "nci.conf"
	echo "$username
$fname
" >> nci.conf
	echo "Configuration file updated."
fi

#Create preferences file (DO NOT DELETE IT)
if [ ! -f "user.conf" ]; then
	echo "$editor" >> user.conf
	echo "Preferences file (\"prefs.conf\") created! (DO NOT DELETE IT!)"
else
	echo "Preferences file already exists. Updating it..."
	rm -f "user.conf"
	touch "user.conf"
	echo "$editor" >> user.conf
	echo "Preferences file updated."
fi

#Create excluded articles file (DO NOT DELETE IT)
if [ ! -f "exclude.conf" ]; then
        touch "exclude.conf"
        echo "exclude.conf file created! (DO NOT DELETE IT!)"
else
        echo "exclude.conf file already exists"
fi

echo "IMPORTANT: Your generated HTML files contain some <!-- COMMENTS LIKE THIS ONE --> that nci system requires to work properly. DO NOT DELETE THEM UNLESS OTHERWISE NOTED."
echo "OK."
